package com.xxw.yygh.cmn.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xxw.yygh.cmn.dao.DictDao;
import com.xxw.yygh.model.cmn.Dict;
import com.xxw.yygh.vo.cmn.DictEeVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;

public class DictListener extends AnalysisEventListener<DictEeVo> {

    private DictDao dictDao;

    public DictListener(DictDao dictDao){
        this.dictDao = dictDao;
    }

    @Override
    public void invoke(DictEeVo dictEeVo, AnalysisContext analysisContext) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictEeVo,dict);

        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",dictEeVo.getId());
        Integer count = this.dictDao.selectCount(queryWrapper);

        if (count > 0){
            //如果id已经存在就进行更新操作
            this.dictDao.updateById(dict);
        }else {
            //id不存在就进行新增操作
            this.dictDao.insert(dict);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
